﻿namespace Yom.WebService.Admin
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class AdminVolume : AdminBase
    {
        public void OutWrite()
        {
            string Action = Factory.Request("Action");
            string CID = Factory.Request("CID");
            string ID = Factory.Request("ID");
            string VolumeName = Factory.Request("VolumeName");
            string OrderID = Factory.Request("OrderID");
            StringBuilder builder;

            if (!string.IsNullOrEmpty(CID))
            {
                switch (Action)
                {
                    case "Add":
                        if (string.IsNullOrEmpty(VolumeName))
                        {
                            builder = new StringBuilder();
                            builder.Append("<fieldset class=\"note\"><dl><dd>分卷标题</dd>");
                            builder.Append("<dt><input id=\"volumename\" type=\"text\" class=\"txt\" /></dt></dl>");
                            builder.Append("<dl><dd>分卷排序</dd><dt><input type=\"text\" class=\"txt\" id=\"orderid\" value=\"0\" /></dt></dl>");
                            builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"添加分卷\" class=\"button\" /></div>");
                            base.Response.Write(builder.ToString());
                        }
                        else
                        {
                            Yom.Model.Volume model = new Yom.Model.Volume();
                            model.ComicID = CID.ToLong();
                            model.OrderID = OrderID.ToInt();
                            model.VolumeName = VolumeName;

                            base.Response.Write((new Yom.Data.Volume().Add(model) ? "1" : "0"));
                        }
                        break;
                    case "Edit":
                        if (!string.IsNullOrEmpty(ID))
                        {
                            if (string.IsNullOrEmpty(VolumeName))
                            {
                                Yom.Model.Volume model = new Yom.Data.Volume().GetModel(ID.ToLong());
                                builder = new StringBuilder();
                                builder.Append("<fieldset class=\"note\"><dl><dd>分卷标题</dd>");
                                builder.Append("<dt><input id=\"volumename\" type=\"text\" class=\"txt\" value=\"" + model.VolumeName + "\" /></dt></dl>");
                                builder.Append("<dl><dd>分卷排序</dd><dt><input type=\"text\" class=\"txt\" id=\"orderid\" value=\"" + model.OrderID.ToString() + "\" /></dt></dl>");
                                builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"修改分卷\" class=\"button\" /></div>");
                                base.Response.Write(builder.ToString());
                            }
                            else
                            {
                                Yom.Model.Volume model = new Yom.Data.Volume().GetModel(ID.ToLong());
                                model.OrderID = OrderID.ToInt();
                                model.VolumeName = VolumeName;

                                base.Response.Write((new Yom.Data.Volume().Update(model) ? "1" : "0"));
                            }
                        }
                        break;
                    case "Del":
                        if (!string.IsNullOrEmpty(ID))
                        {
                            List<Yom.Model.Chapter> list1 = new Yom.Data.Chapter().GetList(ID.ToLong());
                            foreach (Yom.Model.Chapter model in list1)
                            {
                                try
                                {
                                    Fso.DeleteFile(model.ImgPath);
                                }
                                catch (Exception ex) { }
                                new Yom.Data.Chapter().Delete(model.ChapterID);
                            }
                            try
                            {
                                string dirPath = System.IO.Path.Combine(Factory.RootPath, "Files/Cartoon/" + new Yom.Data.Volume().GetModel(ID.ToLong()).ComicID.ToString() + "/" + ID);
                                Fso.DeleteDirectory(dirPath);
                            }
                            catch (Exception ex) { }
                            base.Response.Write((new Yom.Data.Volume().Delete(ID.ToLong()) ? "1" : "0"));
                        }
                        break;
                    case "UploadFile":
                        builder = new StringBuilder();
                        builder.Append("<script type=\"text/javascript\" src=\"Images/Js/jquery.uploadify.js\"></script>");
                        builder.Append("<div id=\"progress\" style=\"color:red\"></div><br /><p>点击Browser选择文件, 点击Start开始上传, 提示: 请将要上传的文件按照01,02,03这样的顺序进行命名,将会按照图片上传顺序进行章节添加</p><input type=\"button\" id=\"Start\" value=\"开始上传\" class=\"button\" />&nbsp;&nbsp;<input type=\"file\" id=\"chapter\" name=\"chapter\" />");
                        base.Response.Write(builder.ToString());
                        break;
                    default:
                        builder = new StringBuilder();
                        string P = Factory.Request("P");
                        int page = (string.IsNullOrEmpty(P) ? 1 : ((P == "0") ? 1 : P.ToInt()));
                        builder.Append("<div class=\"SubMenu\"><a href=\"javascript:$.Run(6,0,0," + CID + ")\">分卷管理</a> | <a href=\"javascript:$.Run(6,1," + page.ToString() + "," + CID + ")\">添加分卷</a></div>");
                        builder.Append("<div align=\"left\" style=\"padding:5px;\">");
                        builder.Append("<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"TablePanel\">");
                        builder.Append("<tr align=\"center\" class=\"TDHead\"><td nowrap>分卷名称</td><td nowrap>所属漫画</td><td nowrap>图片数量</td><td nowrap>分卷排序</td><td nowrap>&nbsp;</td></tr>");
                        List<Yom.Model.Volume> list = new Yom.Data.Volume().GetList(13, page, CID.ToLong(), "", true);
                        foreach (Yom.Model.Volume model in list)
                        {
                            builder.Append("<tr>");
                            builder.AppendFormat("<td align=\"center\"><a href=\"javascript:$.Run(6,2,{0},{3},{1})\">{2}</a></td>", page, model.VolumeID, model.VolumeName, CID);
                            builder.Append("<td align=\"center\">" + new Yom.Data.Comic().GetModel(model.ComicID).ComicName + "</td>");
                            builder.AppendFormat("<td align=\"center\"><a href=\"javascript:$.Run(7,0,0,{0})\">{1}</a></td>", model.VolumeID, new Yom.Data.Chapter().GetCount(model.VolumeID));
                            builder.Append("<td align=\"center\">" + model.OrderID.ToString() + "</td>");
                            builder.AppendFormat("<td align=\"center\"><img border=\"0\" src=\"Images/Admin/top.png\" width=\"16\" height=\"16\" alt=\"增加图片\" onclick=\"$.Run(6,4,{0},{2},{1})\" style=\"cursor:pointer\" />&nbsp;<img border=\"0\" src=\"Images/Admin/del.gif\" width=\"16\" height=\"16\" onclick=\"$.Run(6,3,{0},{2},{1})\" alt=\"删除分卷\" style=\"cursor:pointer\" /></td>", page, model.VolumeID, CID);
                            builder.Append("</tr>");
                        }
                        builder.Append("</table><br /><div id=\"ShowMsg\" style=\"color:red\"></div>");
                        builder.Append("<div class=\"more\">" + Factory.GetPager(13, page, new Yom.Data.Volume().GetCount(CID.ToLong()), "javascript:$.Run(6,0,<{Page}>," + CID + ")") + "</div>");
                        base.Response.Write(builder.ToString());
                        break;
                }
            }
        }
    }
}